#!/bin/bash -
#=======================================================================
#
#          FILE: arp_flush.sh
#         USAGE: ./arp_flush.sh
#   DESCRIPTION: Flush Linux ARP table
#  REQUIREMENTS: ARP(8)
#          BUGS: N/A
#         NOTES: N/A
#        AUTHOR: Ali Ayoub
#         EMAIL: ali@mellanox.com
#       COMPANY: N/A
#       CREATED: 09.17.2010-15:03:39
#      REVISION: 1.1
#=======================================================================

script_name=`basename $0`
out="/tmp/${script_name}.out.$RANDOM"
touch $out

# checks
if [ $UID -ne 0 ]; then
        echo "-E- You must be root to run this script"
        exit 10
elif ! ( \touch $out ); then
        echo "-E- couldn't create $out"
        exit 13
fi

# run arp
\arp -na &> $out

_IFS=$IFS
IFS=`echo -en "\n\b"`;

for i in `cat $out`; do
	ip=`echo $i | awk '{print $2}' | tr -d ")" | tr -d "("`
	if=`echo $i | sed 's/\[ether\]//'  | awk '{print $6}'`

	if [ "$if" == "" ]; then
		if=`echo $i | awk '{print $3}'`
	fi

	if [ "$ip" == "Address" ];then
		continue
	fi
	
	echo arp -d $ip -i $if
	arp -d $ip -i $if
done;

IFS=$_IFS

\rm $out

